package com.serkol.ams.controller.api;

import cn.hutool.extra.pinyin.PinyinUtil;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.serkol.ams.bean.MyPage;
import com.serkol.ams.bean.Result;
import com.serkol.ams.entity.Famous;
import com.serkol.ams.entity.Messages;
import com.serkol.ams.service.FamousService;
import com.serkol.ams.service.MessagesService;
import com.serkol.annotation.IsLogin;
import com.serkol.constant.ResultConstant;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("api/messages")
public class MessagesApiController {
    @Autowired
    private MessagesService messagesService;
    @Autowired
    private FamousService famousService;

    @IsLogin
    @GetMapping("/findByList")
    public Result findByList() {

        Result result = new Result();
        List<Messages> list = messagesService.findByPage();
        if (!list.isEmpty()) {
            result.setStatus(ResultConstant.SUCSESS);
            result.setData(list);
        } else {
            result.setStatus(ResultConstant.EMPTIY);
        }
        return result;
    }
    @GetMapping("/maps")
    public Result maps() {
    Map<String,Map<String, String>>   data=messagesService.findByMaps();
        return Result.ok(data);


    }

    @IsLogin
    @RequestMapping("edit")
    public Result edit(@RequestBody Messages data) {

        boolean b = false;

            b = messagesService.updateById(data);

        return b ? Result.ok() : Result.failure(ResultConstant.FAIL);
    }

    @PostMapping("/serach")
    public Result serach(@RequestBody JSONObject data) {
         String name = data.getString("name");
        System.out.println("--------------------"+name+"--------------------");
        System.out.println(name);
        String pinyin =isChinese(name)? PinyinUtil.getPinyin(name,""):name;
       pinyin= StringUtils.trimToEmpty(pinyin);
        System.out.println("--------------------"+pinyin+"--------------------");
       LambdaQueryWrapper <Messages> query = new LambdaQueryWrapper<>();
       query.eq(Messages::getCode,pinyin);
        if (messagesService.count(query)==0) {
            Messages messages=new Messages();
            messages.setCode(pinyin);
            messages.setZhCn(name);
            messagesService.save(messages);
        }
        return Result.ok();


    }
    public boolean isChinese(String strName) {
        char[] ch = strName.toCharArray();
        for (char c : ch) {
            if (isChinese(c)) {
                return true;
            }
        }
        return false;
    }

    private boolean isChinese(char c) {
        Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
        if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
                || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
                || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
                || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION
                || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
                || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {
            return true;
        }
        return false;
    }

    @IsLogin
    @GetMapping("/findByFamousList")
    public Result findByFamousList(
            @RequestParam(name = "pageSize", defaultValue = "10") int pageSize, @RequestParam(name = "current", defaultValue = "0") int current,
            @RequestParam(name = "state", defaultValue = "0") int state
    ) {

        Result result = new Result();

        return famousService.findByPage(pageSize, current, state);
    }


    @IsLogin
    @PostMapping("editFamous")
    public Result editFamous(@RequestBody Famous data) {

        boolean b = false;

data.setState(1);
        b = famousService.updateById(data);

        return b ? Result.ok() : Result.failure(ResultConstant.FAIL);
    }
    @IsLogin
    @PostMapping("stateFamous")
    public Result stateFamous(

            @RequestParam(name = "id", defaultValue = "0") long id,
            @RequestParam(name = "state", defaultValue = "1") int state
    ) {
        boolean b = false;
        if (id>0) {


            LambdaUpdateWrapper<Famous> query = new LambdaUpdateWrapper<>();
            query.eq(Famous::getId, id).set(Famous::getState, state);

            b = famousService.update(query);
        }
        return b ? Result.ok() : Result.failure(ResultConstant.FAIL);
    }

}
